<template>
  <div class="dialog">
    <el-dialog
      :title="'维修节点配置'+titleText"
      :visible.sync="showDialog"
      width="35%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
      <el-form-item label="节点类型：" class="w100p" prop="processNodeType">
          <el-select
            v-model="formData.processNodeType"
            clearable
            filterable
            placeholder="节点类型选择"
          >
            <el-option
              v-for="item of processNodeType"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否审核：" class="w100p" prop="audit">
          <el-radio-group v-model="formData.audit">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否处理：" class="w100p" prop="handle">
          <el-radio-group v-model="formData.handle">
            <el-radio :label="true">是</el-radio>
            <el-radio
              v-if="formData.processNodeType !== '报废'"
            :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import nodeConfigApi from "@/api/equip-repair-manage/node-config";
import userApi from "@/api/system-settings/user-manage";

export default {
  name: '',
  components: { },
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {
          audit: true,
          handle: true
        }
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        processNodeType: [
          { required: true, message: "请选择节点类型", trigger: ["change", "blur"] }
        ],

        audit:[
          { required: true, message: "请选择是否审核", trigger: ["change", "blur"] }
        ],
        handle:[
          { required: true, message: "请选择是否处理", trigger: ["change", "blur"] }
        ]
      },
      userList: [],
      processNodeTypeList: [
        {
          name: '委内维修',
          value: '委内维修'
        },
        {
          name: '委外维修',
          value: '委外维修'
        },
        {
          name: '报废',
          value: '报废'
        },
      ],
      showDialog: this.show,
      nodeConfigList: []
    };
  },
  computed: {
    processNodeType() {
      // console.log(this.resArr(this.processNodeTypeList, this.nodeConfigList, 'value', 'processNodeType'), 'nodeConfigApi')
      return  this.resArr(this.processNodeTypeList, this.nodeConfigList,'processNodeType', 'value' );
    }
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      this.formData = this.editObject
      this.$nextTick(() => {
        this.$refs['ruleForm']?.clearValidate();
      });
    }
  },
  created() {
    this.queryUserList()
    this.queryList()
  },
  mounted() {
  },
  methods: {
    resArr(arr1, arr2, id1, id2) {
      return arr1.filter((v) => arr2.every((val) => val[id1]!= v[id2]));
    },
    // 查询可选择维修人
    queryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.userList = data;
        }
      });
    },
     // 查询可选择维修人
    async queryList() {
      // userApi.queryUserAll({}).then((res) => {
      //   if (res.code === 1) {
      //     let data = res.data || [];
      //     this.userList = data;
      //   }
      // });
    const { data: data } = await nodeConfigApi.queryPageList({ page: 1, size: 100, entity: { organizationNo: sessionStorage.organizationNo },})
    // console.log(data, 'nodeConfigApi')
      this.nodeConfigList = data.content;
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          this.formData.organizationNo = sessionStorage.organizationNo
          nodeConfigApi.add(this.formData).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg || "添加成功！");
              this.queryList();
              this.close()
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          return false;
        }
      });
    },
    close() {
      this.formData = {}
                this.$nextTick(() => {
        this.$refs['ruleForm']?.clearValidate();
      });
      this.$emit("closeDialog");
    },
    cancel() {
      this.formData = {}
                this.$nextTick(() => {
        this.$refs['ruleForm']?.clearValidate();
      });
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep {
  .el-radio-group {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .el-radio__input.is-checked .el-radio__inner {

    border-color: #006e91;
    background: #006e91;


  }
  .el-radio__input.is-checked+.el-radio__label {
    color: #006e91;

  }
}
</style>
